package Form;

import java.awt.EventQueue;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.FlowLayout;
import javax.swing.JPanel;

import Class.ConnectDatabase;
import Class.Procedure;

import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.JRadioButton;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.LineBorder;

public class Employee_Management {

	private JFrame frmEmployeeManagement;
	public static JTextField tfLoginNameEmpSearch;
	public static JTextField tfLoginNameEmpAdd;
	public static JTextField tfEmpNameAdd;
	public static JTextField tfEmpAddressAdd;
	public static JTextField tfAliasNameAdd;
	public static JPasswordField pfAliasAdd;
	public static JTextField tfEmpPhoneAdd;
	public static JPasswordField pfEmpAdd;
	public static JPasswordField pfConfirmEmp;
	public static JPasswordField pfConfirmAlias;
	public static JLabel lbMessNotFound;
	private JTable table;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Employee_Management window = new Employee_Management();
					window.frmEmployeeManagement.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Employee_Management() {
		ConnectDatabase.connection();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmEmployeeManagement = new JFrame();
		frmEmployeeManagement.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 11));
		frmEmployeeManagement.getContentPane().setForeground(new Color(165, 42, 42));
		frmEmployeeManagement.setTitle("Employee Management");
		frmEmployeeManagement.getContentPane().setBackground(new Color(169, 169, 169));
		frmEmployeeManagement.setBackground(Color.PINK);
		frmEmployeeManagement.setBounds(100, 100, 743, 617);
		frmEmployeeManagement.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmEmployeeManagement.getContentPane().setLayout(null);
		
		JLabel lblEmployeeManagement = new JLabel(" Employee Management");
		lblEmployeeManagement.setForeground(Color.BLUE);
		lblEmployeeManagement.setBounds(280, 5, 204, 17);
		lblEmployeeManagement.setFont(new Font("Tahoma", Font.BOLD, 16));
		frmEmployeeManagement.getContentPane().add(lblEmployeeManagement);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(95, 158, 160));
		panel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBounds(10, 33, 308, 134);
		frmEmployeeManagement.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lbLoginNameEmpDel = new JLabel("Login Name");
		lbLoginNameEmpDel.setForeground(new Color(165, 42, 42));
		lbLoginNameEmpDel.setFont(new Font("Tahoma", Font.BOLD, 13));
		lbLoginNameEmpDel.setBounds(22, 25, 73, 23);
		panel.add(lbLoginNameEmpDel);
		
		tfLoginNameEmpSearch = new JTextField();
		tfLoginNameEmpSearch.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tfLoginNameEmpSearch.setBounds(119, 26, 171, 20);
		panel.add(tfLoginNameEmpSearch);
		tfLoginNameEmpSearch.setColumns(10);
		
		lbMessNotFound = new JLabel("Login Name NOT FOUND !!!");
		lbMessNotFound.setForeground(Color.RED);
		lbMessNotFound.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbMessNotFound.setBounds(77, 109, 157, 14);
		lbMessNotFound.setVisible(false);
		panel.add(lbMessNotFound);
		
		JButton btnDeleteEmp = new JButton("Delete");
		btnDeleteEmp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(!tfLoginNameEmpSearch.getText().trim().equals("")){
					Procedure.DelEmp(tfLoginNameEmpSearch.getText());
					tfLoginNameEmpSearch.setText("");
					
				}else{
					JOptionPane.showMessageDialog(null, "Blank must be filled!!");
				}
			}
		});
		btnDeleteEmp.setBackground(new Color(70, 130, 180));
		btnDeleteEmp.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnDeleteEmp.setBounds(10, 75, 89, 23);
		panel.add(btnDeleteEmp);
		
		JButton btnShowEmpInfo = new JButton("Show");
		btnShowEmpInfo.setBackground(new Color(65, 105, 225));
		btnShowEmpInfo.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnShowEmpInfo.setBounds(208, 75, 89, 23);
		panel.add(btnShowEmpInfo);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(95, 158, 160));
		panel_1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel_1.setBounds(335, 33, 382, 457);
		frmEmployeeManagement.getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lbLoginNameEmpAdd = new JLabel("Login Name");
		lbLoginNameEmpAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbLoginNameEmpAdd.setBounds(21, 23, 75, 21);
		panel_1.add(lbLoginNameEmpAdd);
		
		tfLoginNameEmpAdd = new JTextField();
		tfLoginNameEmpAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tfLoginNameEmpAdd.setBounds(162, 24, 181, 20);
		panel_1.add(tfLoginNameEmpAdd);
		tfLoginNameEmpAdd.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblPassword.setBounds(21, 54, 63, 21);
		panel_1.add(lblPassword);
		
		JLabel lbEmployeeName = new JLabel("Employee Name");
		lbEmployeeName.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbEmployeeName.setBounds(21, 117, 110, 21);
		panel_1.add(lbEmployeeName);
		
		tfEmpNameAdd = new JTextField();
		tfEmpNameAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tfEmpNameAdd.setBounds(162, 117, 181, 20);
		panel_1.add(tfEmpNameAdd);
		tfEmpNameAdd.setColumns(10);
		
		JLabel lbEmployeeAddressAdd = new JLabel("Employee Address");
		lbEmployeeAddressAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbEmployeeAddressAdd.setBounds(21, 149, 110, 19);
		panel_1.add(lbEmployeeAddressAdd);
		
		tfEmpAddressAdd = new JTextField();
		tfEmpAddressAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tfEmpAddressAdd.setBounds(162, 148, 181, 20);
		panel_1.add(tfEmpAddressAdd);
		tfEmpAddressAdd.setColumns(10);
		
		JLabel lbAliasNameAdd = new JLabel("Alias Name");
		lbAliasNameAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbAliasNameAdd.setBounds(21, 212, 80, 14);
		panel_1.add(lbAliasNameAdd);
		
		tfAliasNameAdd = new JTextField();
		tfAliasNameAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tfAliasNameAdd.setBounds(162, 209, 181, 20);
		panel_1.add(tfAliasNameAdd);
		tfAliasNameAdd.setColumns(10);
		
		JLabel lbAliasPasswordAdd = new JLabel("Alias Password");
		lbAliasPasswordAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbAliasPasswordAdd.setBounds(21, 243, 110, 14);
		panel_1.add(lbAliasPasswordAdd);
		
		pfAliasAdd = new JPasswordField();
		pfAliasAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		pfAliasAdd.setBounds(162, 240, 181, 20);
		panel_1.add(pfAliasAdd);
		pfAliasAdd.setColumns(10);
		
		JLabel lbEmployeePhoneAdd = new JLabel("Employee Phone");
		lbEmployeePhoneAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbEmployeePhoneAdd.setBounds(21, 178, 110, 21);
		panel_1.add(lbEmployeePhoneAdd);
		
		tfEmpPhoneAdd = new JTextField();
		tfEmpPhoneAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		tfEmpPhoneAdd.setBounds(162, 178, 181, 20);
		panel_1.add(tfEmpPhoneAdd);
		tfEmpPhoneAdd.setColumns(10);
		
		JLabel lbDesignationAdd = new JLabel("Decentralization");
		lbDesignationAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbDesignationAdd.setBounds(21, 299, 110, 21);
		panel_1.add(lbDesignationAdd);
		
		final JRadioButton rdbtnAdminChoiceAdd = new JRadioButton("Admin");
		rdbtnAdminChoiceAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		rdbtnAdminChoiceAdd.setBounds(21, 327, 75, 23);
		panel_1.add(rdbtnAdminChoiceAdd);
		
		final JRadioButton rdbtnWorkingChoiceAdd = new JRadioButton("Working Department");
		rdbtnWorkingChoiceAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		rdbtnWorkingChoiceAdd.setBounds(200, 327, 143, 23);
		panel_1.add(rdbtnWorkingChoiceAdd);
		
		final JRadioButton rdbtnDispatchingChoiceAdd = new JRadioButton("Dispatching Department\r\n");
		rdbtnDispatchingChoiceAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		rdbtnDispatchingChoiceAdd.setBounds(19, 364, 161, 23);
		panel_1.add(rdbtnDispatchingChoiceAdd);
		
		final JRadioButton rdbtnEnquiryChoiceAdd = new JRadioButton("Enquiry Department");
		rdbtnEnquiryChoiceAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		rdbtnEnquiryChoiceAdd.setBounds(200, 364, 143, 23);
		panel_1.add(rdbtnEnquiryChoiceAdd);
		
		JButton btnAddEmp = new JButton("Add");
		btnAddEmp.setBackground(new Color(0, 139, 139));
		btnAddEmp.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnAddEmp.setBounds(148, 416, 89, 23);
		panel_1.add(btnAddEmp);
		
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(rdbtnAdminChoiceAdd);
		buttonGroup.add(rdbtnWorkingChoiceAdd);
		buttonGroup.add(rdbtnEnquiryChoiceAdd);
		buttonGroup.add(rdbtnDispatchingChoiceAdd);
		
		pfEmpAdd = new JPasswordField();
		pfEmpAdd.setFont(new Font("Tahoma", Font.PLAIN, 13));
		pfEmpAdd.setBounds(162, 55, 181, 20);
		panel_1.add(pfEmpAdd);
		
		JLabel lbPasswordConfirmEmp = new JLabel("Confirm Password");
		lbPasswordConfirmEmp.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbPasswordConfirmEmp.setBounds(21, 88, 110, 14);
		panel_1.add(lbPasswordConfirmEmp);
		
		JLabel lbConfirmPasswordAlias = new JLabel("Confirm Password");
		lbConfirmPasswordAlias.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lbConfirmPasswordAlias.setBounds(21, 274, 110, 14);
		panel_1.add(lbConfirmPasswordAlias);
		
		pfConfirmEmp = new JPasswordField();
		pfConfirmEmp.setFont(new Font("Tahoma", Font.PLAIN, 13));
		pfConfirmEmp.setBounds(162, 86, 181, 20);
		panel_1.add(pfConfirmEmp);
		
		pfConfirmAlias = new JPasswordField();
		pfConfirmAlias.setFont(new Font("Tahoma", Font.PLAIN, 13));
		pfConfirmAlias.setBounds(162, 272, 181, 20);
		panel_1.add(pfConfirmAlias);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel_2.setBackground(new Color(255, 240, 245));
		panel_2.setBounds(10, 178, 308, 312);
		frmEmployeeManagement.getContentPane().add(panel_2);
		panel_2.setLayout(null);
		
		table = new JTable();
		table.setBounds(0, 0, 308, 240);
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
			},
			new String[] {
				"New column", "New column", "New column"
			}
		));
		table.setBackground(new Color(238, 232, 170));
		panel_2.add(table);
		
		JButton btnShowDetailEmp = new JButton("Show detail");
		btnShowDetailEmp.setForeground(new Color(165, 42, 42));
		btnShowDetailEmp.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnShowDetailEmp.setBounds(91, 264, 117, 23);
		panel_2.add(btnShowDetailEmp);
		
		
		
		btnAddEmp.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				// TODO Auto-generated method stub
				try {
					
					if(!tfLoginNameEmpAdd.getText().trim().equals("")&&(pfEmpAdd.getPassword().length!=0)&&!tfEmpNameAdd.getText().trim().equals("")&&!tfEmpAddressAdd.getText().trim().equals("")&&!tfEmpPhoneAdd.getText().trim().equals("")){									
						if(pfEmpAdd.getPassword().length>6){	
							
							if(String.valueOf(pfConfirmEmp.getPassword()).equals(String.valueOf(pfEmpAdd.getPassword()))){
								if(pfAliasAdd.getPassword().length==0||pfAliasAdd.getPassword().length>6){
									if(String.valueOf(pfConfirmAlias.getPassword()).equals(String.valueOf(pfAliasAdd.getPassword()))){
										if(rdbtnAdminChoiceAdd.isSelected()){
											try{																						
												//Procedure.AddEmpInfo(2,tfLoginNameEmpAdd.getText(),String.valueOf(pfEmpAdd.getPassword()),tfAliasNameAdd.getText(),String.valueOf(pfAliasAdd.getPassword()),tfEmpNameAdd.getText(),tfEmpAddressAdd.getText(),Integer.parseInt(tfEmpPhoneAdd.getText()));																					
												rdbtnAdminChoiceAdd.setSelected(false);
												//EmpSucceeded();
												}catch(Exception e){
												
												}
										}else if(rdbtnWorkingChoiceAdd.isSelected()){
											try{																						
												//Procedure.AddEmpInfo(4,tfLoginNameEmpAdd.getText(),String.valueOf(pfEmpAdd.getPassword()),tfAliasNameAdd.getText(),String.valueOf(pfAliasAdd.getPassword()),tfEmpNameAdd.getText(),tfEmpAddressAdd.getText(),Integer.parseInt(tfEmpPhoneAdd.getText()));																					
												rdbtnWorkingChoiceAdd.setSelected(false);
												//EmpSucceeded();
												}catch(Exception e){
													
												}
										}else if(rdbtnDispatchingChoiceAdd.isSelected()){
											try{																						
												//Procedure.AddEmpInfo(5,tfLoginNameEmpAdd.getText(),String.valueOf(pfEmpAdd.getPassword()),tfAliasNameAdd.getText(),String.valueOf(pfAliasAdd.getPassword()),tfEmpNameAdd.getText(),tfEmpAddressAdd.getText(),Integer.parseInt(tfEmpPhoneAdd.getText()));																					
												rdbtnDispatchingChoiceAdd.setSelected(false);
												//EmpSucceeded();
												}catch(Exception e){
													
												}
										}else if(rdbtnEnquiryChoiceAdd.isSelected()){
											try{																						
												//Procedure.AddEmpInfo(3,tfLoginNameEmpAdd.getText(),String.valueOf(pfEmpAdd.getPassword()),tfAliasNameAdd.getText(),String.valueOf(pfAliasAdd.getPassword()),tfEmpNameAdd.getText(),tfEmpAddressAdd.getText(),Integer.parseInt(tfEmpPhoneAdd.getText()));																					
												rdbtnEnquiryChoiceAdd.setSelected(false);
												//EmpSucceeded();
												}catch(Exception e){
													
												}
										}else{
											JOptionPane.showMessageDialog(null, "Please choose Decentralization");
										}
									}else{
										JOptionPane.showMessageDialog(null, "Confirmed password of Alias must be the same as the one above!!!");
									}
								}else{
									JOptionPane.showMessageDialog(null, "Password of Alias length must be more than 6 !!!");
								}
							}else{
								JOptionPane.showMessageDialog(null, "Confirmed password must be the same as the one above!!!");
							}
						}else{
							JOptionPane.showMessageDialog(null, "Password of Employee length must be more than 6 !!!");
						}
					}else{
						JOptionPane.showMessageDialog(null, "You may miss Login name, Password, Name, Address or Phone number");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
	}
	
	public static void EmpSucceeded(){
		tfLoginNameEmpAdd.setText("");
		pfEmpAdd.setText("");
		pfConfirmEmp.setText("");
		tfEmpNameAdd.setText("");
		tfEmpAddressAdd.setText("");
		tfEmpPhoneAdd.setText("");
		tfAliasNameAdd.setText("");
		pfAliasAdd.setText("");
		pfConfirmAlias.setText("");
		JOptionPane.showMessageDialog(null, "Added Successfully!!!");
	}
	
	public static void CheckSameName(){
		JOptionPane.showMessageDialog(null, "Login Name or Alias Name existed !");
	}
	
	public static void CheckSearchedName(){
		JOptionPane.showMessageDialog(null, "Deleted Successfullu!");
	}
}
